$(function(){
	/*原始集合数量*/
	window.index = $("#placeTime").children('div').length;
});

/*删除集合框*/ 
function removePlace(index){
 	$("#place"+index).remove();
}

/*集合增行*/
function updatePlaceTables(){
	var table = "";
	table +=
	'<div id="place'+index+'">地点：'
	+'<input name="placeName" id="placeName'+index+'" type="text" list="PlaceNameList" />'
	+'时间：<input type="text" name="placeTime" id="placeTime'+index+'" maxlength="128" required="required" />'
	+'<input type="button" class="btn" value="删除" onclick="removePlace('+index+')"></div>'
	$("#placeTime").append(table);
	index++;
}

/*提交 */
function saveCreateSurrounding() {
	var type = $("#type").val();
	var placeCount = $("#placeTime").children('div').length;
	var arrPlace = [];
	for(var i=0;i<placeCount;i++){
		var id = $("#placeTime").children('div')[i].id;
		var nameID = $("#"+id).children('input')[0].id;
		var timeID = $("#"+id).children('input')[1].id;
		var place = {};
		place['PlaceName'] = $("#"+nameID).val();
		place['PlaceTime'] = $("#"+timeID).val();
		arrPlace.push(place);
	}
	var lineBase = {};
	lineBase['LineName'] = $("#LineName").val();	
	lineBase['ZoneID'] = $("#LineZoneID").val();
	lineBase['ItineraryPath'] = $("#LineItineraryPath").val();
	lineBase['Own'] = $("#LineOwn").val();
	lineBase['Tips'] = $("#LineTips").val();
	lineBase['Explain'] = $("#LineExplain").val();
	lineBase['Standard'] = $("#LineStandard").val();
	lineBase['Memo'] = $("#LineMemo").val();
	var arr = [];
	arr.push(lineBase);
	var arrs = [];
	var objTable = $("#tripTable tbody");
	for (var i = 1; i < objTable.length; i++) {
		var id=objTable[i].id;
		var num = id.substr(id.lastIndexOf("y") + 1);
		var lineTrip = {};
		lineTrip['Day'] = $("#TripDay" + num).val();
		lineTrip['Address'] = $("#TripAddress" + num).val();
		lineTrip['Stay'] = $("#TripStay" +num).val();
		lineTrip['Meal'] = $("#TripMeal" + num).val();
		lineTrip['Trip'] = $("#TripTrip" +num).val();
		lineTrip['HotelPath1'] = $("#TripHotel1" + num).val();
		lineTrip['HotelPath2'] = $("#TripHotel2" + num).val();
		lineTrip['HotelPath3'] = $("#TripHotel3" +num).val();
		lineTrip['HotelPath4'] = $("#TripHotel4" + num).val();
		lineTrip['ScenicPath1'] = $("#TripScenic1" +num).val();
		lineTrip['ScenicPath2'] = $("#TripScenic2" + num).val();
		lineTrip['ScenicPath3'] = $("#TripScenic3" +num).val();
		lineTrip['ScenicPath4'] = $("#TripScenic4" +num).val();
		lineTrip['Memo'] = $("#TripMemo" +num).val();
		arrs.push(lineTrip);
	}
	$("#form").form("submit",{
		url : "/mmy/savecreatesurrouding",
		type:"post",
        queryParams : {
			"base" : JSON.stringify(arr),
			"trip" : JSON.stringify(arrs),
			"place": JSON.stringify(arrPlace),
			"type":type
		},
		dataType : "text",
		onSubmit:function(){
			return check();
		},
		success : function(data) {
			var txt = "创建成功";
			window.wxc.xcConfirm(txt, window.wxc.xcConfirm.typeEnum.success,{onOk:function(){
				window.location.href = "surroudingtourismindex";
			}});
		},
		error : function() {
			var txt = "创建失败";
			window.wxc.xcConfirm(txt, window.wxc.xcConfirm.typeEnum.error);``
		}
	});
	
}
/*表单空值校验*/
function check(){
	var objTable = $("#tripTable tbody");
	if(objTable.length==1){
		layer.msg('请添加行程', {icon: 5});
		$("#btn_addtr").click();
		return false;
	}
	var arr = new Array(
  		new Array('LineName','线路名不可为空!'),
  		new Array('dist','所属区域不可为空!'),
		new Array('LineOwn','自费项目不可为空!'),
		new Array('LineTips','温馨提示不可为空!'),
		new Array('LineStandard','接待标准不可为空!'),
		new Array('LineExplain','接待说明不可为空!'),	
	)
	for (var i = 1; i < objTable.length; i++) {
		var id=objTable[i].id;
		var num = id.substr(id.lastIndexOf("y") + 1);
		arr.push(new Array('TripAddress'+num,'第'+$("#TripDay"+num).val()+'天的地点为空'));
		arr.push(new Array('TripStay'+num,'第'+$("#TripDay"+num).val()+'天的住宿为空'));
		arr.push(new Array('TripMeal'+num,'第'+$("#TripDay"+num).val()+'天的含餐为空'));
		arr.push(new Array('TripTrip'+num,'第'+$("#TripDay"+num).val()+'天的行程为空'));
	}
	if(!checkForm(arr)){
  		return false;
	}	
}

//行程增行
function updateTripTables(){
	var length=$("#tripTable").children().length-1;
	var id=$("#tripTable").children()[length].id;
	var count=Number(id.substr(id.lastIndexOf("y") + 1))+1;
 	var tableHtml ="";
 tableHtml += 
'<tbody id="tbody'+count+'">'
+'<tr>'
+'<th rowspan="11"><label for="TripDay'+count+'">第</label><input type="number" class="daywidth" id="TripDay'+count+'" name="Day" size="2" maxlength="2"  value="'+Number(length+1)+'" required="required"/><label for="TripDay'+num+'">天</label><span class="mustWrite">*</span></th>'
+'<th ><label for="TripAddress'+count+'">地点</label></th>'
+'<td ><input type="text" id="TripAddress'+count+'" name="Address" maxlength="128" required="required"/><span class="mustWrite">*</span></td>'
+'<th ><label for="TripStay'+count+'">住宿</label></th>'
+'<td ><input type="text" id="TripStay'+count+'" name="Stay" maxlength="128" required="required"/><span class="mustWrite">*</span></td>'
+'<th ><label for="TripMeal'+count+'">含餐</label></th>'
+'<td ><input type="text" id="TripMeal'+count+'" name="Meal" maxlength="64" required="required"/><span class="mustWrite">*</span></td>'
+'<td rowspan="11"><input type="button"  class="btn" onclick="removeTr('+count+')" value="删除"></td> '
+'</tr>'
+'<tr>'
+'<th ><label for="TripTrip'+count+'">具体行程</label></th>'
+'<td colspan="5"><textarea id="TripTrip'+count+'" name="Trip" maxlength="512" required="required"></textarea><span class="mustWrite">*</span></td>'
+'</tr>'
+'<tr>'
+'<th rowspan="4"><label for="TripHotel1'+count+'">酒店照片</label></th>'
+'<td><label for="TripHotel1'+count+'">图片1:</label></td>'
+'<td colspan="4" ><input type="file" id="TripHotel1'+count+'" name="HotelPath1" /></td>'
+'</tr>'
+'<tr>'
+'<td><label for="TripHotel2'+count+'">图片2:</label></td>'
+'<td colspan="4" ><input type="file" id="TripHotel2'+count+'" name="HotelPath2" /></td>'
+'</tr>'
+'<tr>'
+'<td><label for="TripHotel3'+count+'">图片3:</label></td>'
+'<td colspan="4" ><input type="file" id="TripHotel3'+count+'" name="HotelPath3" /></td>'
+'</tr>'
+'<tr>'
+'<td><label for="TripHotel4'+count+'">图片4:</label></td>'
+'<td colspan="4" ><input type="file" id="TripHotel4'+count+'" name="HotelPath4" /></td>'
+'</tr>'
+'<tr>'
+'<th rowspan="4"><label for="TripScenic1'+count+'">景点照片</label></th>'
+'<td><label for="TripScenic1'+count+'">图片1:</label></td>'
+'<td colspan="4" ><input type="file" id="TripScenic1'+count+'" name="ScenicPath1" /></td>'
+'</tr>'
+'<tr>'
+'<td><label for="TripScenic2'+count+'">图片2:</label></td>'
+'<td colspan="4" ><input type="file" id="TripScenic2'+count+'" name="ScenicPath2" /></td>'
+'</tr>'
+'<tr>'
+'<td><label for="TripScenic3'+count+'">图片3:</label></td>'
+'<td colspan="4" ><input type="file" id="TripScenic3'+count+'" name="ScenicPath3" /></td>'
+'</tr>'
+'<tr>'
+'<td><label for="TripScenic4'+count+'">图片4:</label></td>'
+'<td colspan="4" ><input type="file" id="TripScenic4'+count+'" name="ScenicPath4" /></td>'
+'</tr>'
+'<tr>'
+'<td><label for="TripMemo'+count+'">备注:</label></td>'
+'<td colspan="5" ><textarea id="TripMemo'+count+'" name="Memo" maxlength="256"></textarea></td>'
+'</tr>'
+'</tbody>';
   
  var elements = $("#tripTable").children().length; //表示id为“mtTable”的标签下的子标签的个数
   
  $("#tripTable").children().eq(elements-1).after(tableHtml); //在表头之后添加空白行
  length++;  
  count++;

}	